package com.whmskj.xjlhsc.qianYiData.job;

import com.whmskj.xjlhsc.qianYiData.service.The21stService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

@Slf4j
@Component
public class The21stJob {

    @Resource
    private The21stService the21stService;

    /**
     * 每天00:20按“21~20”口径聚合写入 itsThe21StDayOfTheDay。
     * month1 取归属月份的一号：
     *  - 若今日<=20：窗口=上月21~本月20，month1=本月1号
     *  - 若今日>20 ：窗口=本月21~下月20，month1=下月1号
     */
    @Scheduled(cron = "0 20 0 * * ?")
    public void syncByRuleDaily() {
        LocalDate base = LocalDate.now();
        try {
            int affected = the21stService.syncByBaseDate(base);
            log.info("The21stJob[daily]: base={}, affected={}", base, affected);
        } catch (Exception e) {
            log.error("The21stJob[daily]: base={} failed", base, e);
        }
    }

    /**
     * 可选的午间修正（若数据早上才落齐），按需保留/删除。
     */
    @Scheduled(cron = "0 20 12 * * ?")
    public void syncByRuleNoon() {
        LocalDate base = LocalDate.now();
        try {
            int affected = the21stService.syncByBaseDate(base);
            log.info("The21stJob[noon]: base={}, affected={}", base, affected);
        } catch (Exception e) {
            log.error("The21stJob[noon]: base={} failed", base, e);
        }
    }
}

